/*
 * Engine2d.h
 *
 *  Created on: 23-01-2012
 *      Author: Jarek
 */

#ifndef ENGINE2D_H_
#define ENGINE2D_H_

#include <math.h>
#include <jni.h>
#include <GlobalData.h>
#include <EMatrix4x4.h>
#include <android/asset_manager.h>
#include "libpng/png.h"
#include "FreetypeWrapper/GLCallBatcher.h"
#include "FreetypeWrapper/FTBitmapFont.h"

	class Texture;
    class FTBitmapFont;
    class FontAtlas;

    enum alignType {
        alignNone,
        alignLeft,
        alignRight,
        alignTop,
        alignBottom,
        alignCenter,
        alignLeftTop,
        alignLeftBottom,
        alignRightTop,
        alignRightBottom,
    	alignMiddle
    };

    //Fonts alligning values
    #define ALIGN_FONT_LEFT		0x0001
    #define ALIGN_FONT_RIGHT	0x0002
    #define ALIGN_FONT_CENTER	0x0004
    #define ALIGN_FONT_TOP		0x0008
    #define ALIGN_FONT_BOTTOM	0x0010
    #define ALIGN_FONT_MIDDLE	0x0020

class Engine2d {
	friend class Engine;

	int             screenWidth1;
    int             screenHeight1;

    int             minX,minY;
    int             maxX,maxY;

public:

    int             screenWidth;
    int             screenHeight;


    static bool				cos;
    static AAsset			*filePng;

	Engine2d();
	~Engine2d();

	void					setEngine2dSize(int w,int h);
	void					clipImage(int *clipX, int *clipY, int *clipX2, int *clipY2);

	void            		fillRect(int x,int y,int w,int h);
	void					fillTriangle(int x1, int y1, int w, int h);
	void					fillPyramid(int x1, int y1, int z1, int w, int h);

	//Freetype
protected:
	FontAtlas       *fontAtlas;
	GLCallBatcher   batcher;
    uint            utf8bufferSize;     ///< Size of utf8buffer.
    wchar_t         *utf8buffer;        ///< Buffer for char => wchar_t conversion in drawFont(). By default it is set to NULL. If buffer is smaller then char array it is reallocated.

public:
    float           globalScale;        ///< Global scale of drawn texts. Scale passed in drawFont() is multiplied by it. By default it is equal 1.0f. It can be changed at any time.
    float           alignHeight;        ///< Workaround for aligning text correctly, as height is counted differntly in Freetype then in Vtech. Basically, this should be set to height of average letter in the font.

protected:
    wchar_t         *bufferedConvertUtf8ToWchar(const char *text);

public:

    virtual void    initFreetype() = 0; ///< Remember to create FontAtlas and buffer for char/wchar_t conversion.
    void            drawFreetypeAtlas(int x = 0, int y = 0);
    void            drawFont(FTBitmapFont *font, const wchar_t *text, int x, int y, int align = ALIGN_FONT_LEFT | ALIGN_FONT_TOP, float scale  = 1.0f, int tracking = 0, float angle = 0);
    void            drawFont(FTBitmapFont *font, const char    *text, int x, int y, int align = ALIGN_FONT_LEFT | ALIGN_FONT_TOP, float scale  = 1.0f, int tracking = 0, float angle = 0);

    int				getFontHeight(FTBitmapFont *font);
	int				getTxtLen(FTBitmapFont *fonts, const char    *text, float scale, int tracking);
	int				getTxtLen(FTBitmapFont *fonts, const wchar_t *text, float scale, int tracking);
	int				fontParse(FTBitmapFont *fonts, char *aStr, char **aTab, int width, int tracking);
	int				fontParseLines(FTBitmapFont *fonts, char *aStr, int width, int tracking);

	void 			setFontAtlas(FontAtlas *fontA)     { fontAtlas = fontA; }
    FontAtlas       *getFontAtlas()     { return fontAtlas; }
    GLCallBatcher   *getGLCallBatcher() { return &batcher;  }
};

#endif /* ENGINE2D_H_ */
